package com.hlyj.http.base.tool.lib_hlyj_base.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;

import java.io.File;
import java.math.BigDecimal;
import java.util.UUID;

public class Tool {

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.isConnected();
        }
        return false;
    }

    /**
     * 格式化文件大小
     *
     * @param size file.length() 获取文件大小
     */
    public static String formatFileSize(double size) {
        if (size < 0) {
            return "0KB";
        }
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return size + "B";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }


    public static String getFileExtension(File file) {
        if (file != null) {
            String name = file.getName();
            int lastIndex = name.lastIndexOf('.');
            if (lastIndex > 0 && lastIndex < name.length() - 1) {
                return name.substring(lastIndex + 1);
            }
        }
        return "";
    }


    public static long lastTotalRxBytes;
    public static long lastTimeStamp;


    public static long getTotalRxBytes(int uid) {
        //转为KB
        return TrafficStats.getUidRxBytes(uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);
    }

    /**
     * 下载速度字符串格式化
     *
     * @param speed
     * @return
     */
    public static String speedFormat(int speed) {
        String result;
        if (speed > 1024) {
            int partA = speed / 1024;
            int partB = (speed - partA * 1024) / 100;
            result = partA + "." + partB + "m/s";
        } else {
            result = speed + "kb/s";
        }
        return result;
    }

    /**
     * 获取网络下载速度
     *
     * @param uid
     * @return
     */
    public static int getNetSpeed(int uid) {
        long nowTotalRxBytes = getTotalRxBytes(uid);
        long nowTimeStamp = System.currentTimeMillis();
        long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换

        lastTimeStamp = nowTimeStamp;
        lastTotalRxBytes = nowTotalRxBytes;

        int result = 0;
        try {
            result = (int) speed;
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * 应用第一次启动生成，后续拿这个值
     */
    public static String createUdi() {
        // 生成 UDI
        String udi = MD5.digest(UUID.randomUUID().toString());
        return udi;
    }


}
